package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.RentInfo;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.RentInfoService;
import com.hzdl.car.util.AJAXResult;
import com.hzdl.car.vo.CarVo;
import com.hzdl.car.vo.RentInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/rentInfo")
public class RentInfoController {

    @Autowired
    private RentInfoService ris;

    @RequestMapping("/rentInfoInit")
    public String rentInfoInit(){
        return "/system/rentInfo";
    }
    @RequestMapping("/rentInfoShow")
    @ResponseBody
    public AJAXResult rentInfoShow(CarVo carVo){
        if(carVo.getPage()==null){
            carVo.setPage(1);
        }
        if(carVo.getLimit()==null){
            carVo.setLimit(5);
        }
        PageInfo pageInfo=ris.findAllCar(carVo);
        return new AJAXResult(0,"",pageInfo.getList(),pageInfo.getTotal());
    }
    @RequestMapping("/checkRent")
    @ResponseBody
    public AJAXResult checkRent(String identity, HttpSession session){
        int code = ris.selectByIndentity(identity);
        if(code!=0){
            session.setAttribute("identity",identity);
        }
         return new AJAXResult(code);
    }
    @RequestMapping("/generateInfo")
    @ResponseBody
    public AJAXResult generateInfo(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createtime=sdf1.format(new Date());
        map.put("createtime",createtime);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        String l="ZC_";
        Random r = new Random();
        for(int i =0;i<6;i++){
           l+=r.nextInt(10);
        }
        l+="_";
        for(int j =0;j<4;j++){
            l+=r.nextInt(10);
        }
        l+="_";
        for(int k =0;k<5;k++){
            l+=r.nextInt(10);
        }
        l+=sdf2.format(new Date());
        map.put("rentNumber",l);
//        String identity1=(String)session.getAttribute("identity");
//        map.put("identity",identity1);
        User user = (User)session.getAttribute("user");
        map.put("realname",user.getRealname());
        map.put("user",user);
        return new AJAXResult(1,"",map);
    }
    @RequestMapping("/addRentInfo")
    @ResponseBody
    public AJAXResult addRentInfo(RentInfoVo rentInfoVo){
        int row=ris.rentCar(rentInfoVo.getCarnumber());
        row+=ris.insertRentInfo(rentInfoVo);
        if(row==0){
            return new AJAXResult(1,"出租失败");
        }
        return new AJAXResult(1,"出租成功");
    }
}
